/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simhya.simengine;
import simhya.model.flat.FlatModel;
import simhya.dataprocessing.DataCollector;
import simhya.simengine.stochastic.*;
import simhya.simengine.stochastic.PureGuardedSSASimulator;
import simhya.GlobalOptions;

/**
 *
 * @author Luca
 */
public class SimulatorFactory {


    public static Simulator newSimulator(SimType type, FlatModel model, DataCollector collector) {
        switch (type) {
            case SSA:
                return SimulatorFactory.newSSAsimulator(model, collector);
            case GB:
                return SimulatorFactory.newGBsimulator(model, collector);
            default:
                throw new UnsupportedOperationException(type.toString() + " to be implemented");
        }
    }

    public static Simulator newSSAsimulator(FlatModel model, DataCollector collector) {
        if (model.containsDelayedTransitions())
            return new simhya.simengine.stochastic.SSASimulatorWithDelays(model, collector);
        else if (model.containsInstantaneousTransitions() || model.containsTimedTransitions())
            return new simhya.simengine.stochastic.SSASimulator(model, collector);
        else if (model.containsGuardedTransitions()) {
            PureGuardedSSASimulator sim = new simhya.simengine.stochastic.PureGuardedSSASimulator(model, collector);
            if (model.getNumberOfTransitions() - model.getAverageNumberOfTransitionsToUpdate() 
                    <= GlobalOptions.thresholdForUsingDependencyGraph)
                sim.setUseDependencyGraph(false);
            return sim;
        }
        else {
            PureSSASimulator sim  = new simhya.simengine.stochastic.PureSSASimulator(model, collector);
            if (model.getNumberOfTransitions() - model.getAverageNumberOfTransitionsToUpdate()
                    <= GlobalOptions.thresholdForUsingDependencyGraph)
                sim.setUseDependencyGraph(false);
            return sim;
        }
        /*
         * TODO: heuristic to deactivate dependency graph in pure SSA simulator/PureGuardedSSA
         */
    }

    public static Simulator newGBsimulator(FlatModel model, DataCollector collector) {
        if (model.containsDelayedTransitions())
            return new simhya.simengine.stochastic.GBSimulatorWithDelays(model, collector);
        else if (model.containsInstantaneousTransitions() || model.containsTimedTransitions())
            return new simhya.simengine.stochastic.GBSimulator(model, collector);
        else if (model.containsGuardedTransitions())
            return new simhya.simengine.stochastic.PureGuardedGBSimulator(model, collector);
        else
            return new simhya.simengine.stochastic.PureGBSimulator(model, collector);
    }

}
